import {autoAddDictionary} from '~/plugins/Tools/autoStore';
import {DeepCopy} from '~/plugins/Tools';
const dictionary = { workflowAuditorType: [], workflowConditionType: [], workflowType: [] };

function defaultData_start () {
  return {
    workFlowBranchPojoList: [
      {
        workflowBranchDetailVos: [],
        workflowBranchVo: {
          bpmnNodeId: 'start_user_task_id',
          branckName: '默认条件',
          isDefault: 2,
          remark: '审核通过后执行下一个流程',
          toBranch: 'end_event_id',
        },
      },
    ],
    workflowAudiorVo: [],
    workflowNodeVo: {
      workFlowNodeName: '报销人',
      bpmnNodeId: 'start_user_task_id',
    },
  };
}

function defaultData_end () {
  return {
    workFlowBranchPojoList: [],
    workflowAudiorVo: [],
    workflowNodeVo: {
      workFlowNodeName: '流程结束',
      bpmnNodeId: 'end_event_id',
    },
  };
}

function defaultData () {
  return [
    defaultData_start(),
    defaultData_end(),
  ];
}

export const state = () => ({
  data: defaultData(),
  ...autoAddDictionary('state', dictionary),
});

/**
 * GROUP/SET
 *
 */

export const getters = {
  tree: function (state) {
    let step = {};
    state.data.forEach(item => {
      step[item.workflowNodeVo.bpmnNodeId] = item;
    });
    return step;
  },
  option: function (state) {
    let step = [];
    let length = state.data.length - 1;
    state.data.forEach((item, index) => {
      if (length !== index) {
        let i = DeepCopy(item);
        i.workflowNodeVo.nodeLeave = index;
        step.push(i);
      }
    });
    return step;
  },
  auditors: function (state) {
    let step = [];
    let length = state.data.length - 1;
    state.data.forEach((item, index) => {
      if (length !== index) {
        let i = DeepCopy(item);
        let workflowNodeId = item.workflowNodeVo.id;
        i.workflowAudiorVo.forEach(iitem => {
          step.push({
            ...iitem,
            workflowNodeId,
          });
        });
      }
    });
    return step;
  },
};
export const mutations = {
  SET_data: function (state, data) {
    if (!data) {
      console.error('尝试 传入空的 data ：WORKFLOW SET_data');
      return;
    }
    if (data.constructor === Array) {
      data.push(defaultData_end());
      state.data = data;
    } else {
      let {node, parentNode} = data;
      const nodeId = node.workflowNodeVo.bpmnNodeId;
      const parentId = parentNode.workflowNodeVo.bpmnNodeId;
      let parentIndex = state.data.findIndex(item => {
        return item.workflowNodeVo.bpmnNodeId === parentId;
      });
      const nextNodeId = state.data[parentIndex + 1].workflowNodeVo.bpmnNodeId;
      state.data.splice(parentIndex + 1, 0, node);
      let tree = {};
      state.data.forEach(item => {
        tree[item.workflowNodeVo.bpmnNodeId] = item;
      });
      tree[parentId].workFlowBranchPojoList[0].workflowBranchVo.toBranch = nodeId;
      tree[nodeId].workFlowBranchPojoList[0].workflowBranchVo.toBranch = nextNodeId;
    }
  },
  PUT_data: function (state, data) {
    if (!data) {
      console.error('尝试 传入空的 data ：WORKFLOW PUT_data');
      return;
    }
    const node = data;
    const nodeId = node.workflowNodeVo.bpmnNodeId;
    const nodeIndex = state.data.findIndex(item => {
      return item.workflowNodeVo.bpmnNodeId === nodeId;
    });
    for (let index in state.data[nodeIndex]) {
      if (state.data[nodeIndex].hasOwnProperty(index)) {
        state.data[nodeIndex][index] = node[index];
      }
    }
  },
  DEL_data: function (state) {
    state.data = defaultData();
  },
  DEL_node: function (state, node) {
    if (!node) {
      console.error('尝试 传入空的 node ：WORKFLOW DEL_node');
      return;
    }
    const nodeId = node.workflowNodeVo.bpmnNodeId;
    const nodeIndex = state.data.findIndex(item => {
      return item.workflowNodeVo.bpmnNodeId === nodeId;
    });
    state.data.splice(nodeIndex, 1);
    const nodeNextIndex = nodeIndex;
    const parentNodeIndex = nodeIndex - 1;
    const nodeNext = state.data[nodeNextIndex];
    const parentNode = state.data[parentNodeIndex];
    parentNode.workFlowBranchPojoList[0].workflowBranchVo.toBranch = nodeNext.workflowNodeVo.bpmnNodeId;
  },
  ...autoAddDictionary('mutations', dictionary),
};
